<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$

date_default_timezone_set('PRC'); //设置中国时区
set_time_limit(0);
error_reporting(0);
/**
 *
 */
class Da {
    //要登录的考试平台的账号
    public static $username = '123456';
    //要登录的考试平台的账号
    public static $password = '123456';
    //接受邮件的QQ
    public static $QQ = '123456@qq.com';
    public function __construct() {
        $this->curl();
        $this->index();
    }
    function curl() {
        $post = array(
            'username' => self::$username,
            'password' => self::$password,
        );
        $url = "http://172.16.10.111/exam/gologin.do";
        $cookie = dirname(__FILE__) . '/cookie_oschina.txt';
        $this->login_post($url, $cookie, $post);
        $new = '';
        for ($i = 1; $i < 5; $i++) {
            $url1 = "http://172.16.10.111/exam/exam/studentScoreList1.do?query=true&currentPage=" . $i;
            $arr_new1 = $this->get_content($url1, $cookie);
            $new.= $arr_new1 . $i;
        }
        $reg = '/<tr>.*?<td.*?>理论<\/td>.*?<td.*?>.*?<a.*?onClick=(.*?)><i class="fa"><\/i><span style="color: blue">查看试卷<\/span><\/a>.*?<\/td>.*?<\/tr>.*?/s';
        preg_match_all($reg, $new, $data);
        foreach ($data[1] as $key => $value) {
            $new_arr[$key] = explode(',"', $value);
            $new_arrs[$key] = explode('"', $new_arr[$key][4]);
            $new_arrs[$key] = $new_arrs[$key][0];
        }
        $paperid = $this->curl_post("http://140.143.29.201/index.php/index/index/curl", $new_arrs);
        $data = json_decode($paperid);
        $this->data($data);
    }
    function data($paperid) {
        $post = array(
            'username' => self::$username,
            'password' => self::$password,
        );
        foreach ($paperid as $key => $value) {
            $url = "http://172.16.10.111/exam/gologin.do";
            $cookie = dirname(__FILE__) . '/cookie_oschina.txt';
            $url1 = 'http://172.16.10.111/exam/exam/viewExamPaper.do?paperId=' . $value;
            $this->login_post($url, $cookie, $post);
            $a = $this->get_content($url1, $cookie);
            $reg = '/.*?<pre>(.*?)<\/pre>.*?/i';
            $regA = '/<span class="question-li-text">A:(.*?)<\/span>/s';
            $regB = '/<span class="question-li-text">B:(.*?)<\/span>/s';
            $regC = '/<span class="question-li-text">C:(.*?)<\/span>/s';
            $regD = '/<span class="question-li-text">D:(.*?)<\/span>/s';
            $dataa = '/<span>正确答案：<\/span>.*?<span class="answer_value">(.*?)<\/span><br>/s';
            preg_match_all($reg, $a, $datas);
            preg_match_all($regA, $a, $data_A);
            preg_match_all($regB, $a, $data_B);
            preg_match_all($regC, $a, $data_C);
            preg_match_all($regD, $a, $data_D);
            preg_match_all($dataa, $a, $data_DD);
            $num = count($data_C[1]);
            $countNum = count($data_DD[1]);
            foreach ($data_A[1] as $k => $value) {
                $sql_arr[$key][$k]['title_name'] = $datas[1][$k] . '(选择)';
                $sql_arr[$key][$k]['title_A'] = $this->trimall($data_A[1][$k]);
                $sql_arr[$key][$k]['title_B'] = $this->trimall($data_B[1][$k]);
                $sql_arr[$key][$k]['title_C'] = $this->trimall($data_C[1][$k]);
                $sql_arr[$key][$k]['title_D'] = $this->trimall($data_D[1][$k]);
                $sql_arr[$key][$k]['title_data'] = $this->trimall($data_DD[1][$k]);
            }
            for ($i = $num; $i < $countNum; $i++) {
                @$sql_data[$i][$i]['title_name'] = $datas[1][$i] . '(判断或填空)';
                $sql_data[$i][$i]['title_data'] = $this->trimall($data_DD[1][$i]);
            }
            foreach ($sql_data as $key => $value) {
                $sql_arr[0][$key] = $value[$key];
            }
        }
        @$sql_arr = array(
            json_encode($sql_arr)
        );
        @$data = $this->SendDataByCurl('http://140.143.29.201/index.php/index/index/addtitle', $sql_arr);
    }
    function index() {
        $post = array(
            'username' => self::$username,
            'password' => self::$password,
        );
        $url = "http://172.16.10.111/exam/gologin.do";
        $cookie = dirname(__FILE__) . '/cookie_oschina.txt';
        $this->login_post($url, $cookie, $post);
        $url1 = "http://172.16.10.111/exam/exam/list.do";
        $arr_new1 = $this->get_content($url1, $cookie);
        if (empty($arr_new1)) {
            $date = date('Y-m-d H:i:s', time());
            echo '<div width="100%;height:700px;"><center style="font-size:100px;"><br>' . $date . '</center><center style="font-size:100px;line-height:700px;"><a href="http://172.16.10.111/exam/login.do">账号或密码输入错误！</a></center></div>';
            die;
        }
        $reg = '/<tr>.*?<td>理论<\/td>.*?<a.*?href="(.*?)" class="btn btn-success btn-sm join-practice-btn">参加考试<\/a>.*?.*?<\/tr>/s';
        preg_match_all($reg, $arr_new1, $data);
        @$arr_new1 = $this->get_content($data[1][0], $cookie);
        if (empty($arr_new1)) {
            $date = date('Y-m-d H:i:s', time());
            echo '<div width="100%;height:700px;"><center style="font-size:100px;"><br>' . $date . '</center><center style="font-size:100px;line-height:700px;"><a href="http://172.16.10.111/exam/login.do">平台没有试卷！</a></center></div>';
            die;
        }
        $file = './Fzzz';
        if (!file_exists($file)) {
            mkdir($file);
        }
        $pagename = date('m月d日H时', time()) . '~学号' . self::$username . '.html';
        $pagename = iconv("UTF-8", "GBK", $pagename);
        $fileName = $file . '/' . $pagename;
        if (!is_file($fileName)) {
            file_put_contents($fileName, $arr_new1);
        }
        $reg = '/.*?<pre>(.*?)<\/pre>.*?/s';
        preg_match_all($reg, $arr_new1, $new_title);
        $reg = '/.*?<span class="question-no">(.*?)<\/span>/s';
        preg_match_all($reg, $arr_new1, $titleNum);
        $new_title[1]['username'] = self::$username;
        $new_title[1]['QQ'] = self::$QQ;
        $data = $this->curl_post("http://140.143.29.201/", $new_title[1]);
        if ($data == 1) {
            $date = date('Y-m-d H:i:s', time());
            echo '<div width="100%;height:700px;"><center style="font-size:100px;"><br>' . $date . '</center><center style="font-size:100px;line-height:700px;"><a href="http://172.16.10.111/exam/login.do">邮件发送成功！</a></center></div>';
        } else {
            $date = date('Y-m-d H:i:s', time());
            echo '<div width="100%;height:700px;"><center style="font-size:100px;"><br>' . $date . '</center><center style="font-size:100px;line-height:700px;"><a href="http://172.16.10.111/exam/login.do">邮件发送失败！</a></center></div>';
        }
    }
    function curl_post($url, $post) {
        $options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $post,
        );
        $ch = curl_init($url);
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    function SendDataByCurl($url, $data = array()) {
        $url = str_replace(' ', '+', $url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "$url");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $output = curl_exec($ch);
        $errorCode = curl_errno($ch);
        curl_close($ch);
        if (0 !== $errorCode) {
            return false;
        }
        return $output;
    }
    function trimall($str) {
        $qian = array(
            " ",
            "　",
            "\t",
            "\n",
            "\r"
        );
        $hou = array(
            "",
            "",
            "",
            "",
            ""
        );
        return str_replace($qian, $hou, $str);
    }
    function login_post($url, $cookie, $post) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        curl_exec($curl);
        curl_close($curl);
    }
    function get_content($url, $cookie) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
        $rs = curl_exec($ch);
        curl_close($ch);
        return $rs;
    }
}
$obj = new Da();

